﻿using Alimentador.Foods;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Alimentador.BottomGroup.Results
{
    public class MineralValue
    {
        private double m_quantity = 0.00;
        private string m_name = "";

        /// <summary>
        /// Constructor with default values
        /// </summary>
        /// <param name="name">string</param>
        /// <param name="quantity">double</param>
        public MineralValue(string name = "", double quantity = 0.00)
        {
            m_name = name;
            m_quantity = quantity;
        }

        public string GetPropertyName
        {
            get
            {
                return "";
                //return m_name;
            }
            set
            {
                m_name = value;
            }
        }

        public double Quantity
        {
            get
            {
                return System.Math.Round(m_quantity, 2);
            }
            set
            {
                m_quantity = value;
            }
        }
    }


    /// <summary>
    /// Interaction logic for MineralsPieChart.xaml
    /// </summary>
    public partial class MineralsColumnChart : UserControl
    {
        public Dictionary<PropertyName, string> m_minerals = new Dictionary<PropertyName, string>()
        {
            {PropertyName.Calcio,   "Cálcio"},
            {PropertyName.Ferro,    "Ferro"},
            {PropertyName.Magnesio, "Magnésio"},
            {PropertyName.Sodio,    "Sódio"},
            {PropertyName.Potassio, "Potássio"},
            {PropertyName.Fosforo,  "Fósforo"},
            {PropertyName.Zinco,    "Zinco"}
        };

        private List<FoodBase> m_foodList = new List<FoodBase>();
        private int m_numDays = 1;
        private static bool m_IsShownInPopUp;

        /// <summary>
        /// Constructor
        /// </summary>
        public MineralsColumnChart()
        {
            InitializeComponent();

            ColumnChartAxis.Maximum = 200;
            ColumnChartAxis.Minimum = 0;

            PopulateChart();
            RefreshChart();
        }

        /// <summary>
        /// Fill the chart with default values
        /// </summary>
        private void PopulateChart()
        {
            ColumnChart.FontSize = 10;

            string str = m_minerals[PropertyName.Calcio];
            seriesCalcium.DataContext = new MineralValue(str, 0);
            seriesCalcium.Title = str;

            str = m_minerals[PropertyName.Ferro];
            seriesFerro.DataContext = new MineralValue(str, 0);
            seriesFerro.Title = str;

            str = m_minerals[PropertyName.Magnesio];
            seriesMagnesium.DataContext = new MineralValue(str, 0);
            seriesMagnesium.Title = str;

            str = m_minerals[PropertyName.Sodio];
            seriesSodium.DataContext = new MineralValue(str, 0);
            seriesSodium.Title = str;
            
            str = m_minerals[PropertyName.Potassio];
            seriesPotassio.DataContext = new MineralValue(str, 0);
            seriesPotassio.Title = str;

            str = m_minerals[PropertyName.Fosforo];
            seriesFosforo.DataContext = new MineralValue(str, 0);
            seriesFosforo.Title = str;

            str = m_minerals[PropertyName.Zinco];
            seriesZinco.DataContext = new MineralValue(str, 0);
            seriesZinco.Title = str;
        }

        /// <summary>
        /// Refresh chart values
        /// </summary>
        private void RefreshChart()
        {
            PersonPatient p = MainWindow.CurrentPatient;

            if (p == null)
            {
                return;
            }

            double max = 100;

            double val = FoodFormulas.PercentageMineralCalcium(m_foodList, MainWindow.CurrentPatient, m_numDays);
            seriesCalcium.DataContext = new MineralValue[] { new MineralValue(m_minerals[PropertyName.Calcio], val) };
            seriesCalcium.Title = m_minerals[PropertyName.Calcio] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.PercentageMineralIron(m_foodList, p, m_numDays);
            seriesFerro.DataContext = new MineralValue[] { new MineralValue(m_minerals[PropertyName.Ferro], val) };
            seriesFerro.Title = m_minerals[PropertyName.Ferro] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.PercentageMineralMagnesium(m_foodList, p, m_numDays);
            seriesMagnesium.DataContext = new MineralValue[] { new MineralValue(m_minerals[PropertyName.Magnesio], val) };
            seriesMagnesium.Title = m_minerals[PropertyName.Magnesio] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.PercentageMineralSodium(m_foodList, p, m_numDays);
            seriesSodium.DataContext = new MineralValue[] { new MineralValue(m_minerals[PropertyName.Sodio], val) };
            seriesSodium.Title = m_minerals[PropertyName.Sodio] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.PercentageMineralPotassium(m_foodList, p, m_numDays);
            seriesPotassio.DataContext = new MineralValue[] { new MineralValue(m_minerals[PropertyName.Potassio], val) };
            seriesPotassio.Title = m_minerals[PropertyName.Potassio] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.PercentageMineralPhosphor(m_foodList, p, m_numDays);
            seriesFosforo.DataContext = new MineralValue[] { new MineralValue(m_minerals[PropertyName.Fosforo], val) };
            seriesFosforo.Title = m_minerals[PropertyName.Fosforo] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.PercentageMineralZinc(m_foodList, p, m_numDays);
            seriesZinco.DataContext = new MineralValue[] { new MineralValue(m_minerals[PropertyName.Zinco], val) };
            seriesZinco.Title = m_minerals[PropertyName.Zinco] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            ColumnChartAxis.Maximum = max;
            ColumnChart.Refresh();
        }

        /// <summary>
        /// Hanlde the event for adding a Food to list
        /// </summary>
        /// <param name="foodList">List(Food)</param>
        /// <param name="numDays">int</param>
        internal void AddFood(List<FoodBase> foodList, int numDays = 1)
        {
            m_foodList = foodList;
            m_numDays = numDays;
            RefreshChart();
        }

        private void pieChart_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (!m_IsShownInPopUp)
            {
                TheMainGrid.Children.Remove(ColumnChart);

                ChartPopUp cpp = new ChartPopUp(ColumnChart);
                cpp.Owner = Application.Current.MainWindow;
                m_IsShownInPopUp = true;
                cpp.ShowDialog();

                ColumnChart = cpp.getChartBack();

                TheMainGrid.Children.Add(ColumnChart);
                m_IsShownInPopUp = false;
            }
        }
    }
}
